//@charset "utf-8";
//选股数据
define(function (require, exports, module) {
	var Backbone = require('backbone');
	var _ = require('_');

	var clientJump = require('../lib/clientjump');

	var stockSearchModel = require('../models/stocksearch');
	var codelistModel = require('../models/codelist');
	var BaseView = require('./base');
	var baseView = new BaseView();

	var StockSearchView = Backbone.View.extend({
		el: '#resultStockView',
		initialize: function() {
			//
		},
		events : {
			'click .view_td_sort' : 'stockSort',
			'dblclick [view_dbl_stock_search]' : 'jumpClientMLine'
		},
		pageSize : 5000,
		request : function (stockCode) {

			$('#noResultView').hide();
			$('#resultStockView').hide();

			var self = this;

			if(!baseView.getQuery('plateid')){
					codelistModel.clearCodeList();
			}
			stockSearchModel.getStockList(stockCode, function () {
				self.render();

				//COCO新增

				//判断有板块id时隐藏条件
				if(baseView.getQuery('plateid')){
						$('#view_result_condition_create').hide();
						$('#view_select_choose').hide();
						$('#view_select_option_container').hide();
				}
			});
		},
		render : function () {
			var self = this;
			var page = 1;

			var result = stockSearchModel.get('result');
			var total = result.total;
			var code = result.code;
			var block = result.block;
			var isSelected = false;
			var selectedCodeList = codelistModel.getCodeList();
			isSelected = true;
			if (1 !== selectedCodeList[code['code']] && baseView.getQuery('plateid')) {
				isSelected = false;
			}
			var zdfStr = '';
			if(code['zhangdiefu'] === '--'){
				zdfStr = '<td class="green">--</td>';
			}else {
				zdfStr = '<td class="'+(code['zhangdiefu'] > 0 ? 'red' : 'green')+'">'+code['zhangdiefu']+'%</td>';
			}

			var codeHtml = [
				'<tr view_click_stat="cop_581208fa_505" class="data-stock '+(isSelected ? 'data-stock-select' : '')+'" data-index="0" data-stock="'+code['code']+'" ',
				'view_dbl_stock_search="'+code['code']+'">',
				'<td style=" text-align: right;"><input type="checkbox" class="selectAll selectRows " '+(isSelected ? 'checked' : '')+' value_code="'+code['code']+'"></td>',
				'<td>00</td>',
				'<td>'+code['code']+'</td>',
				'<td>'+code['zqmc']+'</td>',
				zdfStr,
				'<td class="'+(code['zhangdiefu'] > 0 ? 'red' : 'green')+'">'+code['new']+'</td>',
				'</tr>'
			];

			$('#view_result_condition_create').hide();
			$('#view_result_condition_num').html("选出结果：1个");
			$('#view_cur_stock').html(codeHtml.join(''));
			$('#view_cur_block').html(block['zqmc']+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+total+"只股票");
			$("#resultStockView").height(390);
			self.renderData(page);

			$('#operate').show();
			$('#resultStockView').show();
			$("#view_select_choose_name").removeClass("add-plate-lock");
			// $('.result-table').scrollTop(0);

			baseView.renderScrollTable();
		},
		renderData : function (page) {
			var self = this;
			var result = stockSearchModel.get('result');

			var codelist = result.codelist;
			var pageData = stockSearchModel.getPageData(page, self.pageSize, codelist);

			var num = (page - 1) * self.pageSize + 1;
			var html = [];
			for (var i in pageData.data) {
				var isSelected = false;
				var zdfStr = '';
				if(pageData.data[i]['zhangdiefu'] === '--'){
					zdfStr = '<td class="green">--</td>';
				}else {
					zdfStr = '<td class="'+(pageData.data[i]['zhangdiefu'] > 0 ? 'red' : 'green')+'">'+pageData.data[i]['zhangdiefu']+'%</td>';
				}
				var newprice = parseFloat(pageData.data[i]['newprice']);
				newprice = isNaN(newprice) ? '--' : newprice.toFixed(2);
				var row = [
					'<tr view_click_stat="cop_581208fa_505" class="data-stock '+(isSelected ? 'data-stock-select' : '')+(num % 2 == 0 ? 'bggray':'')+'" ',
					'data-index="'+num+'" data-stock="'+pageData.data[i]['code']+'" ',
					'view_dbl_stock_search="'+pageData.data[i]['code']+'">',
					'<td style=" text-align: right;"><input type="checkbox" class="selectAll selectRows"></td>',
					'<td>'+(num < 10 ? '0'+num : num)+'</td>',
					'<td>'+pageData.data[i]['code']+'</td>',
					'<td>'+pageData.data[i]['zqmc']+'</td>',
					zdfStr,
					'<td class="'+(pageData.data[i]['zhangdiefu'] > 0 ? 'red' : 'green')+'">'+newprice+'</td>',
					'</tr>'
				];
				num++;

				html.push(row.join(''));
			}

			$('#view_code_list').html(html.join(''));
		},
		hideEl : function () {
			this.$el.hide();
		},
		jumpClientMLine : function (e) {
			e = e || window.event;
			var code = $(e.currentTarget).attr('view_dbl_stock_search');
			var stockList = stockSearchModel.getStockListStr('|');
			clientJump.jumpClientStockPage(379, code, stockList);
		},
		//股票列表排序
		stockSort : function(e) {
			var self = this;
			e = e ||window.event;
			var $dom = $(e.currentTarget);
			var type = $dom.attr("type");	//asc||desc
			var field = $dom.attr("field");
			var result = stockSearchModel.get("result");
			var data = stockSearchModel.sortData(field, type, result.codelist, 1);

			self.renderData(1, data);
			$(".sort").each(function(){
				$(this).html("");
			});
			if (type === 'asc') {
				$dom.attr("type", "desc");
				$dom.find(".sort").html("↑");
			} else {
				$dom.attr("type", "asc");
				$dom.find(".sort").html("↓");
			}
		}
	});
	module.exports = new StockSearchView();
});
